var url = null;

$(function() {
	loadStatus();
	query();
});

function loadStatus() {
	$('#status_query').combobox({
		url : getHttpRequestPath("/transport/loadStatus"),
		valueField : 'value',
		textField : 'name'
	});
}

/**
 * 查询
 */
function query() {
	var cmd = new Cmd();
	var transId = $("#transId_query").val();
	cmd.transId = transId.length > 0 ? transId : null;
	var statusCode = $("#status_query").combobox('getValue');
	cmd.statusCode = statusCode.length > 0 ? new Number(statusCode) : 0;
	
	$('#dg').datagrid({
		view: detailview,
		url : getHttpRequestPath("/transport/queryTransList"),
		pageNumber : 1,
		queryParams : {
			cmd : JSON.stringify(cmd, cmd.StringifyReplacer)
		},
		detailFormatter : function(index, row) {
			var ret = '';
			$.ajax({
				url:getHttpRequestPath("/order/getOrderByTransID"),
				data:{
					"transId":row.transId,
				},
				success:function(response) {
					var orders = JSON.parse(response, function(key, value) {
						return value;
					});
					for (var i = 0; i < orders.length; i++) {
						var order = orders[i];
						ret += order.orderId + " - " + order.sku + " * " + order.amount + " - " + order.status + "<br>";
					}
				},
				async:false
			});
			return ret;
		}
	});
}

function formatLine(val, row, index) {
	var str = "";
	$.ajax({
		url:getHttpRequestPath("/transport/getWayLine"),
		data:{
			"wayLine":row.wayLine,
		},
		success:function(response) {
			str += response;
		},
		async:false
	});
	return str;
}

var formatDateStr = "yyyy-MM-dd hh:mm:ss";
function formatDate(val, row, index) {
	var str ="";
	if (row.create.time == "Jan 1, 1970 8:00:00 AM") {
		str += "-<br>";
	} else {
		str += new Date(row.create.time).format(formatDateStr) + "<br>";
	}
	if (row.complete.time == "Jan 1, 1970 8:00:00 AM") {
		str += "-";
	} else {
		str += new Date(row.complete.time).format(formatDateStr);
	}
	return str;
}

function formatDate2(val, row, index) {
	var str ="";
	if (row.delivery.time == "Jan 1, 1970 8:00:00 AM") {
		str += "-<br>";
	} else {
		str += new Date(row.delivery.time).format(formatDateStr) + "<br>";
	}
	if (row.modify.time == "Jan 1, 1970 8:00:00 AM") {
		str += "-";
	} else {
		str += new Date(row.modify.time).format(formatDateStr);
	}
	return str;
}

/**
 * 操作
 */
function formatAction(val, row, index) {
	var str = "";
	if (row.status == 'WAITING') {
		str += createLinkElement('发货', 'delivery(\'' + row.transId + '\')', 'icon-edit');
		str += createLinkElement('取消', 'cancel(\'' + row.transId + '\')', 'icon-cancel');
	}
	if (row.status == 'DELIVERY' && role == 'DELIVERY') {
		str += createLinkElement('配送', 'confirm(\'' + row.transId + '\')', 'icon-edit');
	}
	return str;
}
